﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public static partial class StringExpend
{
    public static string Rewrite(this string str)
    {
        return str.Trim().Replace("(", string.Empty).Replace(")", string.Empty);
    }

    /// <summary>
    /// 字符串是否有效
    /// </summary>
    public static bool IsValid(this string str)
    {
        if (string.IsNullOrEmpty(str)
            || str.ToLower() == "null")
        {
            return false;
        }

        return true;
    }


    /// <summary>
    /// int 是否有效
    /// </summary>
    public static bool IsValid(this int value)
    {
        return value > 0;
    }


    /// <summary>
    /// 安全的转为整型
    /// </summary>
    public static int SafeToInt(this string value)
    {
        int ret = 0;
        int.TryParse(value, out ret);
        return ret;
    }


    /// <summary>
    /// 安全的转为长整型
    /// </summary>
    public static long SafeToLong(this string value)
    {
        long ret = 0;
        long.TryParse(value, out ret);
        return ret;
    }


    /// <summary>
    /// 安全的转为浮点型
    /// </summary>
    public static float SafeToFloat(this string value)
    {
        float ret = 0;
        float.TryParse(value, out ret);
        return ret;
    }


    /// <summary>
    /// 安全转为Vector2类型
    /// </summary>
    public static Vector2 SafeToVector2(this string pos)
    {
        return pos.SafeToVector2(',');
    }


    /// <summary>
    /// 安全转为Vector2类型
    /// </summary>
    public static Vector2 SafeToVector2(this string pos, char split)
    {
        if (!pos.IsValid()) { return Vector2.zero; } // 无效直接返回默认值

        string[] v2Strs = pos.Split(split);
        Vector2 v2 = Vector2.zero;
        if (v2Strs.Length >= 0) v2.x = v2Strs[0].SafeToFloat();
        if (v2Strs.Length >= 1) v2.y = v2Strs[1].SafeToFloat();
        return v2;
    }


    /// <summary>
    /// 安全转为Vector3类型
    /// </summary>
    public static Vector3 SafeToVector3(this string pos)
    {
        return pos.SafeToVector3(',');
    }


    /// <summary>
    /// 安全转为Vector3类型
    /// </summary>
    public static Vector3 SafeToVector3(this string pos, char split)
    {
        if (!pos.IsValid()) { return Vector3.zero; }
        string[] v3Strs = pos.Split(split);
        Vector3 v3 = Vector3.zero;
        if (v3Strs.Length >= 0) v3.x = v3Strs[0].SafeToFloat();
        if (v3Strs.Length >= 1) v3.y = v3Strs[1].SafeToFloat();
        if (v3Strs.Length >= 2) v3.z = v3Strs[2].SafeToFloat();
        return v3;
    }


    /// <summary>
    /// 字符串安全转int数组
    /// </summary>
    public static int[] SafeToIntArray(this string data, char split)
    {
        string[] strRet = data.Split(split);
        int count = strRet.Length;
        int[] ret = new int[count];

        for (int i = 0; i < count; ++i)
        {
            ret[i] = strRet[i].SafeToInt();
        }

        return ret;
    }


    /// <summary>
    /// 安全的格式化字符串
    /// </summary>
    public static string SafeFormat(this string format, params object[] args)
    {
        string ret = string.Empty;

        try
        {
            ret = string.Format(format, args);
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.Message + ex.StackTrace);
        }

        return ret;
    }


    /// <summary>
    /// 对象数组转字符串
    /// </summary>
    public static string ToStringInfo(this object[] array)
    {
        string data = string.Empty;

        if (null == array
            || array.Length <= 0)
        {
            return data;
        }

        for (int i = 0; i < array.Length; ++i)
        {
            data += array[i].ToString();

            if (i != array.Length - 1)
            {
                data += "-";
            }
        }

        return data;
    }
}
